package masterlink.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import masterlink.bean.OrdersBean;
import masterlink.bean.StockBean;
import masterlink.dao.OrdersDAO;
import masterlink.dao.StockDAO;
import masterlink.form.GetTickData;
import masterlink.form.StockDayData;
import masterlink.util.Util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class StockDayDisController {
	
	private static final Logger log=LoggerFactory.getLogger(StockDayDisController.class);
	private OrdersDAO odao;
	private StockDAO sdao;
	
	@Autowired
	public void setOdao(OrdersDAO odao) {
		this.odao = odao;
	}

	@Autowired
	public void setSdao(StockDAO sdao) {
		this.sdao = sdao;
	}
	
	@RequestMapping(value = "/getTickData")
	public String getTickData(GetTickData gtk, HttpServletRequest req){
		int stime = gtk.getTick()-1;
		int etime = gtk.getTick();
		int category = gtk.getCategory();
		int date = gtk.getDate();
		try{
			switch(category){
			case 3:
				category=2;
				break;
			}
			req.setAttribute("list",odao.find(date,category,Util.getTickTime(stime)*100,Util.getTickTime(etime)*100));
		}catch(Exception e){
			log.error("Error in getTickData. ",e);
		}
		return "getTickData";
	}
	
	@RequestMapping(value = "/stockDayDis")
	public String stockDayDis(HttpServletRequest req){
		String category=(Util.isEmpty(req.getParameter("category")))?"2":req.getParameter("category");
		log.trace("stockDayDis category : {}", category);
		try {
			switch(Integer.parseInt(category)){
			case 3:
				req.setAttribute("list", odao.findGroup(2));
				break;
			default:
				req.setAttribute("list", odao.findGroup(Integer.parseInt(category)));
				break;
			}
			req.setAttribute("cat",category);
		} catch (Exception e) {
			log.error("Error in stockDayDis. ",e);
		}
		return "stockDayDis";
	}
	
	@RequestMapping(value = "/getBackTest")
	public String getBackTest(StockDayData data,HttpServletRequest req){
		log.trace("getBackTest {}",data);
		int a[] ;
		TreeMap<Integer,Object> m = new TreeMap<Integer, Object>();
		TreeMap<Integer,Object> m1 = new TreeMap<Integer, Object>();
		if(data.getCategory()==3){
			data.setCategory(2);
		}
		List<OrdersBean> list = new ArrayList<OrdersBean>();
		try {
			list = odao.find(data.getDate(),data.getCategory());
			for(OrdersBean ob:list){
				//a的陣列是存放資料|902|2|0.09|0|0.0|0|0.0|0|0.0|
				//|時間|命中買股數|權重|命中賣股數|權重|未命中股數|權重|持平股數|權重|
				//以分鐘計算進入map
				//改成每20秒計算一次,並且要累加,這個蠻傷腦筋的
				int c=comTime(ob.getTime1());
				//a = (int[]) m.get(c);
				a = (int[]) m1.get(ob.getTime1()/100);
				//if(ob.getBsum()!=0 && ob.getSsum()!=0){
				//	continue;
				//}
				if(a==null){
					a=new int[8];
				}
				switch(ob.getTag()){
				case 0:
					a[4]=a[4]+1;
					a[5]=a[5]+ob.getWeight();
					break;
				case 1:
					if(ob.getBsum()>ob.getSsum()){
						a[0]=a[0]+1;
						a[1]=a[1]+ob.getWeight();
					}else{
						a[2]=a[2]+1;
						a[3]=a[3]+ob.getWeight();
					}
					break;
				case 2:
					a[6]=a[6]+1;
					a[7]=a[7]+ob.getWeight();
					break;
				}
				//這裡把值取出來,不能使用 int b[]=a;
				//因為物件都是使用call by referance
				//所以囉,用了2個Map跟2個陣列去接
				int b[]=new int[8];
				for(int i=0;i<a.length;i++){
					b[i]=a[i];
				}
				m.put(c,b);
				m1.put(ob.getTime1()/100, a);
			}
			//在這裡處理麻煩的累加 0~20秒, 0~40秒, 0~59秒
			//前面已經分開成20,40,59了
			req.setAttribute("m",m);
		} catch (Exception e) {
			log.error("Error in getBackTest. ",e);
		}
		return "getBackTest";
	}
	private int comTime(int time1){
		int a = (time1 /100) * 100;
		int b = time1 % 100;
		int c=0;
		if(b>=0 && b<=20){
			c=20;
		}else if(b>=21 && b<=40){
			c=40;
		}else{
			c=59;
		}
		return a+c;
	}
	
	@RequestMapping(value = "/stockDayData")
	public String stockDayData(StockDayData data,HttpServletRequest req){
		log.trace("stockDayData {}",data);
		List<OrdersBean> list = new ArrayList<OrdersBean>();
		List<StockBean> lists = new ArrayList<StockBean>();
		int d1[]=new int[270];
		int d2[]=new int[270];
		int d3[]=new int[270];
		int d4[]=new int[270];
		int s1[]=new int[271];
		int bCnt=0,sCnt=0;
		try {
			String category="";
			switch(data.getCategory()){
			case 1:
				category="FITX";
				break;
			case 2:
				category="50";
				break;
			case 3:
				category="FITX";
				data.setCategory(2);
				break;
			}
			lists = sdao.find(data.getDate(), category);
			list = odao.find(data.getDate(),data.getCategory());
			for(StockBean sb:lists){
				if(s1[Util.compTimeNum(sb.getTime())]>1){
					continue;
				}
				s1[Util.compTimeNum(sb.getTime())]=sb.getDprice();
			}
			for(OrdersBean ob:list){
				//log.info("time={}, tick={}",ob.getTime1(),ut.compTimeNum(ob.getTime1()));
				switch(ob.getTag()){
				case 0://平手
					//d1[Util.compTimeNum(ob.getTime1())]=d1[Util.compTimeNum(ob.getTime1())]+1;
					d1[Util.compTimeNum(ob.getTime1())]=d1[Util.compTimeNum(ob.getTime1())]+ob.getWeight();
					break;
				case 1://擊中
					if(ob.getBsum()>ob.getSsum()){
						bCnt++;
						//d2[Util.compTimeNum(ob.getTime1())]=d2[Util.compTimeNum(ob.getTime1())]+1;
						d2[Util.compTimeNum(ob.getTime1())]=d2[Util.compTimeNum(ob.getTime1())]+ob.getWeight();
					}else{
						sCnt++;
						//d3[Util.compTimeNum(ob.getTime1())]=d3[Util.compTimeNum(ob.getTime1())]+1;
						d3[Util.compTimeNum(ob.getTime1())]=d3[Util.compTimeNum(ob.getTime1())]+ob.getWeight();
					}
					break;
				case 2://未擊中
					//d4[Util.compTimeNum(ob.getTime1())]=d4[Util.compTimeNum(ob.getTime1())]+1;
					d4[Util.compTimeNum(ob.getTime1())]=d4[Util.compTimeNum(ob.getTime1())]+ob.getWeight();
					break;
				}
			}
			String a1="",a2="",a3="",a4="",a5="";
			for(float a:d1){
				a1 = a1 + a + ",";
			}
			for(float a:d2){
				a2 = a2 + a + ",";
			}
			for(float a:d3){
				a3 = a3 + a + ",";
			}
			for(float a:d4){
				a4 = a4 + a + ",";
			}
			int priceBuf=0;
			/*
			for(int a:s1){
				if(a>0){
					priceBuf=a;
				}else{
					a=priceBuf;
				}
				a5 = a5 + a + ",";
			}
			*/
			for(int i=0;i<s1.length;i++){
				int a=s1[i];
				if(a>0){
					priceBuf=a;
				}else{
					a=priceBuf;
				}
				a5 = a5 + a + ",";
			}
			a1=a1.substring(0,a1.length()-1);
			a2=a2.substring(0,a2.length()-1);
			a3=a3.substring(0,a3.length()-1);
			a4=a4.substring(0,a4.length()-1);
			a5=a5.substring(0,a5.length()-1);
			req.setAttribute("a1", a1);
			req.setAttribute("a2", a2);
			req.setAttribute("a3", a3);
			req.setAttribute("a4", a4);
			req.setAttribute("a5", a5);
			req.setAttribute("o1",data.getA1());
			req.setAttribute("o2",data.getA2());
			req.setAttribute("bCnt",bCnt);
			req.setAttribute("sCnt",sCnt);
			req.setAttribute("o3",data.getA3());
			req.setAttribute("date",data.getDate());
		} catch (Exception e) {
			log.error("Error in stockDayData. ",e);
		}
		return "stockDayData";
	}

}
